export default {
    // vid 选择的id , arr  需要匹配的数组
    getSelectObj(vId, arr) {
        let obj = {};
        obj = arr.find((item) => { //this.ruleForm
            return item.id === vId; //筛选出匹配数据
        });
        return obj
    },

    // 格式化
    format(val) {
        if (!val) {
            return ''
        }
        var now = new Date(val),
            y = now.getFullYear(),
            m = now.getMonth() + 1,
            d = now.getDate();
        return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
    },
    kilo(num) {
        var str = num + '';
        return str.split("").reverse().reduce((prev, next, index) => {
            return ((index % 3) ? next : (next + ',')) + prev;
        })
    },
    // 格式化
    formatDateTime(val) {
        if (!val) {
            return ''
        }
        var now = new Date(val),
            y = now.getFullYear(),
            m = now.getMonth() + 1,
            d = now.getDate();
        return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
    },
    isImage(url) {
        const imageExts = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'];
        let index = url.lastIndexOf(".");
        let ext = url.substr(index + 1);
        return imageExts.indexOf(ext.toLowerCase()) != -1
    },

    // 金额每三位用逗号隔开，保留两位小数
    numFormat(val) {
        val = val.toString().replace(/\$|\,/g, '');
        if (isNaN(val)) {
            val = "0";
        }
        let sign = (val == (val = Math.abs(val)));
        val = Math.floor(val * 100 + 0.50000000001);
        let cents = val % 100;
        val = Math.floor(val / 100).toString();
        if (cents < 10) {
            cents = "0" + cents
        }
        for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
            val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3));
        }

        // if (cents!="00"){
        return (((sign) ? '' : '-') + val + '.' + cents);
        // }else{
        // 	return (((sign) ? '' : '-') + val );
        // }
    },
}